package com.lyfx.ly.service.impl;

import com.lyfx.ly.entity.StudyStepReadContentEntity;
import com.lyfx.ly.service.StudyStepReadContentService;
import com.lyfx.ly.vo.StepDescVo;
import com.lyfx.ly.vo.StudyStepVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.Query;

import com.lyfx.ly.dao.StudyStepDao;
import com.lyfx.ly.entity.StudyStepEntity;
import com.lyfx.ly.service.StudyStepService;


@Service("studyStepService")
public class StudyStepServiceImpl extends ServiceImpl<StudyStepDao, StudyStepEntity> implements StudyStepService {

    @Autowired
    StudyStepReadContentService readContentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StudyStepEntity> page = this.page(
                new Query<StudyStepEntity>().getPage(params),
                new QueryWrapper<StudyStepEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<StudyStepVo> listWithTree(Long courseId) {
        //查出所有的分类
        List<StudyStepEntity> entityList = this.baseMapper.selectList(new QueryWrapper<StudyStepEntity>().eq("course_id", courseId));
        List<StudyStepVo> collect = entityList.stream().map(item -> {
            StudyStepVo studyStepVo = new StudyStepVo();
            BeanUtils.copyProperties(item, studyStepVo);
            return studyStepVo;
        }).collect(Collectors.toList());

        List<StudyStepVo> levelMenu = collect.stream().filter(list ->
                list.getParentSid() == 0
        ).map(menu -> {
            menu.setChildren(getChildrens(menu, collect));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return levelMenu;
    }

    @Override
    public void removeMenusByIds(List<Long> asList) {
        //TODO 检查是否被其他地方引用，
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public StepDescVo getStepContext(Long stepId) {
        StepDescVo descVo = new StepDescVo();
        StudyStepEntity byId = this.getById(stepId);
        descVo.setStepId(byId.getStepId()).setStepName(byId.getStepName()).setReadNum(byId.getReadNum());
        StudyStepReadContentEntity one = readContentService.getOne(new QueryWrapper<StudyStepReadContentEntity>().eq("step_id", stepId));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        if(one!=null){
            descVo.setContTime(simpleDateFormat.format(one.getGmtModified())).setReadContent(one.getReadContent());
        }else {
            descVo.setContTime(simpleDateFormat.format(new Date())).setReadContent("无内容");
        }
        return descVo;
    }

    /**
     * 递归查找所有子步骤
     */
    public List<StudyStepVo> getChildrens(StudyStepVo root,List<StudyStepVo> all){
        List<StudyStepVo> children = all.stream().filter(studyStepVo -> {
            return studyStepVo.getParentSid().compareTo(root.getStepId())==0;
        }).map(item -> {
            item.setChildren(getChildrens(item, all));
            return item;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }
}